package com.wnxy.controller;

import com.github.pagehelper.PageInfo;
import com.wnxy.pojo.User;
import com.wnxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param current 当前页
     * @param size 页大小
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页",required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "size",value = "页大小",required = true,defaultValue = "1")
    })
    public PageInfo<User> findPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "3") Integer size) {
        PageInfo<User> pageInfo = userService.findByPage(current, size);
        return pageInfo;
    }

    @GetMapping("/page2")
    public Map<String,Object> findPage2(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "3") Integer size) {
        PageInfo<User> pageInfo = userService.findByPage(current, size);

        Map<String,Object> map = new HashMap();
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("data",pageInfo.getList());
        return map;
    }
}
